﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using DGen;

namespace Tab2Sql
{
    /// <summary>
    ///
    /// </summary>
    public class Combine
    {

        /// <summary>
        ///
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public string FileLengths()
        {
            string t = "";
            foreach (var m in fSizes)
            {
                t += m.Key +"\t"+ m.Value.ToString()+Environment.NewLine;
            }
            return t;
        }

        private Dictionary<string, int> fSizes;
        private ArrayList fList;
        private int fCount;
        private StringBuilder combinedFile;
        private int combinedLength;
        /// <summary>
        /// Returns the combined length in lines of file.
        /// </summary>
        public int CombinedLength
        {
            get { return combinedLength; }
            set { combinedLength = value; }
        }




        /// <summary>
        ///
        /// </summary>
        public StringBuilder CombinedFile
        {
            get { return combinedFile; }
            set { combinedFile = value; }
        }
        /// <summary>
        ///
        /// </summary>
        public int FCount
        {
          get { return fCount; }
          set { fCount = value; }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="block">
        /// A <see cref="System.String"/>
        /// </param>
        public Combine(string block)
        {
            fSizes = new Dictionary<string, int>();
            
            combinedLength = 0;
            combinedFile = new StringBuilder(block.Length*1048);
            fCount=0;
            fList = new ArrayList();
            string[] splitter = new string[1];
            splitter[0]="\n";
            string[] files = block.Split(splitter,StringSplitOptions.RemoveEmptyEntries);
            foreach (string fName in files)
            {
                if (File.Exists(fName))
                {
                    fList.Add(fName);
                    fCount++;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Process()
        {
            int fNumber = 0;
            foreach (object fN in fList)
            {
                string fN2 = fN.ToString();
                int x = 0;
                string[] fileA = DFile.GetFileAsArray(fN2);
                for (int i = 0; i < fileA.Length; i++)
                {
                    if (i == 0 && fNumber!=0 || fileA[i].Trim().Length<2)
                    {
                        //skip
                    }
                    else
                    {
                        combinedLength++;
                        combinedFile.AppendLine(fileA[i]);
                        x=i;
                    }
                }
                fSizes.Add(fN2, x);
                fNumber++;
            }
           
            
        }

    }
}
